package com.ikkong.sn.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.sn.model.SnSupplier;
import com.ikkong.sn.service.SnSupplierService;
import com.ikkong.sn.service.impl.SnSupplierServiceImpl;

/**
 * Generated by Blade.
 * 2019-04-25 21:36:08
 */
public class SnSupplierController extends UrlPermissController {
	private static String CODE = "snSupplier";
	private static String PERFIX = "sn_supplier";
	private static String LIST_SOURCE = "SnSupplier.list";
	private static String BASE_PATH = "/platform/snSupplier/";
	
	SnSupplierService service = new SnSupplierServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "snSupplier.html");
	}


	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "snSupplier_add.html");
	}

	public void edit() {
		String id = getPara(0);
		SnSupplier snSupplier = service.findById(id);
		setAttr("model", JsonKit.toJson(snSupplier));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "snSupplier_edit.html");
	}

	public void view() {
		String id = getPara(0);
		SnSupplier snSupplier = service.findById(id);
		setAttr("model", JsonKit.toJson(snSupplier));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "snSupplier_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		SnSupplier snSupplier = mapping(PERFIX, SnSupplier.class);
		boolean temp = service.save(snSupplier);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	@ClearShiro
	public void choose(){
		setAttr("code", CODE);
		render(BASE_PATH + "choose_supp.html");
	}

	public void update() {
		SnSupplier snSupplier = mapping(PERFIX, SnSupplier.class);
		boolean temp = service.update(snSupplier);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
        List<String> strings = Arrays.asList(ids.split(","));
        int cnt = 0;
        for(String id :strings){
            Integer sid = Blade.dao().selectSingle("SuSupplier.findCount", Record.create().set("sid", id), Integer.class);
            if(sid<1){
                cnt += service.deleteByIds(ids);
            }
        }

		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
